package com.txzt.gaea.business.fund.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.fund.domain.model.FundApply;
import com.txzt.gaea.business.fund.service.IFundApplyService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/fund/apply")
public class FundApplyController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(FundApplyController.class);

    @Autowired
    private IFundApplyService fundApplyService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param fundApply
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, FundApply fundApply) {
        logger.info(fundApply.toString());
        PageQuery<FundApply> pageList = fundApplyService.pageQuery(new PageQuery<>(pageNumber, pageSize, fundApply));
        pageList.getList().forEach(item -> {
            //翻译部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public RtnInfo<?> queryById(@RequestParam Long id) {
        FundApply fundApply = fundApplyService.selectFundApplyById(id);
        if (null == fundApply) {
            return RtnInfo.SERVER_ERROR;
        }else {
            return RtnInfo.success(fundApply);
        }

    }

    /**
     * 新增
     * @param fundApply
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody FundApply fundApply) {
        int count = fundApplyService.addFundApply(fundApply);
        if (count == 9) {
            return RtnInfo.error(9,"请领名称已存在！");
        }
        if (count == 99) {
            return RtnInfo.error(99,"请领经费超出剩余经费，请领失败！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 编辑
     * @param fundApply
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody FundApply fundApply) {
        int count = fundApplyService.editFundApply(fundApply);
        if (count == 9) {
            return RtnInfo.error(9,"请领名称已存在！");
        }
        if (count == 99) {
            return RtnInfo.error(99,"请领经费超出剩余经费，请领失败！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 通过
     * @param fundApply
     * @return
     */
    @PutMapping(value = "/pass")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> pass(@RequestBody FundApply fundApply) {
        int count = fundApplyService.passFundApply(fundApply);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 退回
     * @param fundApply
     * @return
     */
    @PutMapping(value = "/back")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> back(@RequestBody FundApply fundApply) {
        int count = fundApplyService.backFundApply(fundApply);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 不通过
     * @param fundApply
     * @return
     */
    @PutMapping(value = "/reject")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> reject(@RequestBody FundApply fundApply) {
        int count = fundApplyService.rejectFundApply(fundApply);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        FundApply fundApply = new FundApply();
        fundApply.setId(id);
        fundApply.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = fundApplyService.updateById(fundApply);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


}
